*** LOADING DATA FILE
	cd "C:\Users\\`=c(username)'\Dept of Pol Science Dropbox\Rune Stubager\02_ClassIDscandi\Data\DNES" // replace with your directory
	use "replicationfile_DK.dta", clear

*** ANALYSES 
cd "C:\Users\\`=c(username)'\Dept of Pol Science Dropbox\Rune Stubager\02_ClassIDscandi\Outputs\Final figures" // replace with your directory

*** PART 1: INDIVIDUAL-LEVEL ANALYSES ***

* Creating the Danish part of Figure 1: Distribution of class identity over time
preserve
tabulate classid3s, generate(var)
collapse (sum) var1 var2 var3 var4 var5 var6 var7, by(year)
gen p1 = var1 / (var1 +var2 +var3 +var4 +var5 +var6 +var7)
gen p2 = (var1+var2) / (var1 +var2 +var3 +var4 +var5 +var6 +var7)
gen p3 = (var1+var2+var3) / (var1 +var2 +var3 +var4 +var5 +var6 +var7)
gen p4 = (var1+var2+var3+var4) / (var1 +var2 +var3 +var4 +var5 +var6 +var7)
gen p5 = (var1+var2+var3+var4+var5) / (var1 +var2 +var3 +var4 +var5 +var6 +var7)
gen p6 = (var1+var2+var3+var4+var5+var6) / (var1 +var2 +var3 +var4 +var5 +var6 +var7)
gen p7 = 1
gen zero = 0

twoway rarea zero p2 year, color(gs0) || rarea p2 p5 year, color(gs14) || rarea p5 p7 year, color(gs8) legend(order(1 "Working class ID" 2 "No class ID" 3 "Middle class ID" )) ytitle(Percent) xla(1971 1973 1977 1979 1981 1984 1990 1994 2015 2019 2022, angle(45)) xtitle("") title("Denmark") // saving(classdist3DK, replace)
restore

graph use classdist3DK.gph // 
graph export classdist3DK.png, replace

* Creating the Danish part of Figure 2: Class identification and attitudes to redistribution 

eststo redistdk: reg tax i.classid2##i.year i.agekat gender edu2 i.class
margins year#classid2
marginsplot, title("Denmark") xtitle("") ytitle("Redistribution") recastci(rarea) xlabel(, angle(45)) ciopt(color(%20)) // saving(eclr2multiDK.gph, replace)
// graph export eclr2multiDK.png, replace

* Creating the Danish part of Figure 3: Class identification and attitudes to immigration

eststo immigdk: reg cultthreat i.classid2##i.year i.agekat gender edu2 i.class
margins year#classid2
marginsplot,  title("Denmark") xtitle("") ytitle("Immigration attitudes") recastci(rarea) xlabel(, angle(45)) ylabel(.3(.1).8) ciopt(color(%20)) // saving(img2multiDK.gph, replace)
// graph export img2multiDK.png, replace

* Creating Figure 4: Class id and vote choice

eststo partydk: mlogit party4 i.classid2##ib2022.year i.agekat i.gender i.edu2 i.class

margins year#classid2, predict(outcome(1)) // Old left plot
marginsplot, title("Old Left") xtitle("") ytitle("% support Old Left") xlabel(, angle(45)) recastci(rarea) ylab(0(.2).8) legend(row(1)) ciopt(color(%20)) // saving(oldleft2multiDK.gph, replace) 
// graph export oldleft2multiDK.png, replace

margins year#classid2, predict(outcome(2)) // New left plot
marginsplot, title("New Left") xtitle("") ytitle("% support New Left") xlabel(, angle(45)) recastci(rarea) ylab(0(.1).4) legend(row(1)) ciopt(color(%20)) // saving(newleft2multiDK.gph, replace) 
// graph export newleft2multiDK.png, replace

margins year#classid2, predict(outcome(3)) // Old right plot
marginsplot, title("Old Right") xtitle("") ytitle("% support Old Right") xlabel(, angle(45)) recastci(rarea) ylab(0(.2).8) ciopt(color(%20)) // saving(oldright2multiDK.gph, replace) 
// graph export oldright2multiDK.png, replace

qui mlogit party4 i.classid2##i.year i.agekat i.gender i.edu2 i.class if year>1971
margins year#classid2, predict(outcome(4)) // New right plot
marginsplot, title("New Right") xtitle("") ytitle("% support New Right") xlabel(1971 1973 1977 1979 1981 1984 1990 1994 2015 2019 2022, angle(45)) xscale(r(1971 2022)) recastci(rarea)  ylab(0 (.1) .4) ciopt(color(%20)) // saving(newright2multiDK.gph, replace)
// graph export newright2multiDK.png, replace

grc1leg oldleft2multiDK.gph oldright2multiDK.gph newleft2multiDK.gph newright2multiDK.gph
// graph save party2multiDK.gph, replace
// graph export party2multiDK.png, replace

*** PART 2: ANALYSES OF PARTY POLARIZATION INTERACTION ***

merge m:1 year using "C:\Users\au22016\Dept of Pol Science Dropbox\Rune Stubager\02_ClassIDscandi\Data\DNES\DKmanifest.dta" // replace with your directory
drop if _merge<3

xtset year

gen ecopos= ecoposdk
gen autlib= autlibdk

**Models in main paper, Table 3 (Full model in Table A6.5)

eststo m1dk: xtmlogit party4 i.classid2##c.ecopos i.gender i.edu2 i.agekat i.class if party4<5, re
estimates save "C:\Users\au22016\Dept of Pol Science Dropbox\Rune Stubager\02_ClassIDscandi\Data\m1dk", replace
test 1.classid2#c.ecopos
eststo m2dk: xtmlogit party4 i.classid2##c.autlib i.gender i.edu2 i.agekat i.class if party4<5, re
estimates save "C:\Users\au22016\Dept of Pol Science Dropbox\Rune Stubager\02_ClassIDscandi\Data\m2dk", replace
test 1.classid2#c.autlib
eststo m3dk: xtmlogit party4 i.classid2##c.ecopos i.classid2##c.autlib i.gender i.edu2 i.agekat i.class if party4<5, re
estimates save "C:\Users\au22016\Dept of Pol Science Dropbox\Rune Stubager\02_ClassIDscandi\Data\m3dk", replace
test 1.classid2#c.ecopos
test 1.classid2#c.autlib

reg party4 i.classid2 c.ecopos c.autlib i.gender i.edu2 i.agekat i.class if party4<5, r
estat vif

